package com.sky.directdeal.activity;

import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.sky.directdeal.R;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.fragment.BusinessListFragment;
import com.sky.directdeal.fragment.BusinessMapFragment;
import com.sky.directdeal.helper.BusinessListActivityHelper;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.listener.UiUpdateListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.directdeal.manager.SharedPreferenceManager;
import com.sky.directdeal.utils.Utils;
import com.sky.local_ads.store_list.StoreDetails;
import com.sky.local_ads.store_list.StoreResponse;



public class BusinessListActivity extends BaseFragmentActivity implements OnClickListener {
	private BusinessListFragment businessListFragment;
	private BusinessMapFragment businessMapFragment;
	private boolean isService=false,isMapView=false;
	private RelativeLayout storeListLayout,storeMapLayout;
	private Button storeViewButton;
	private boolean isPlayServiceAvailable=false;
	private int playServiceStatus=-1;
	private int catId=-1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_list);
		setHomeUp();
		businessListFragment=new BusinessListFragment();
		businessMapFragment=new BusinessMapFragment();
		initElements();
		initListFragment();
		getBundleData();
		playServiceStatus = Utils
				.getPlayServiceStatus(this);
		if ( playServiceStatus == ConnectionResult.SUCCESS ) {
			isPlayServiceAvailable=true;
		}
		
		if( isPlayServiceAvailable ){
			initMapFragment();
		}
		
	}
	
	private void getBundleData() {
		if (getIntent() != null
				&& getIntent().hasExtra(Constants.INTENT_CATEGORYY_ID)
				&& getIntent().hasExtra(Constants.INTENT_CATEGORYY_NAME)) {
			
			int catId = getIntent().getIntExtra(Constants.INTENT_CATEGORYY_ID,
					0);
			String catName = getIntent().getStringExtra(
					Constants.INTENT_CATEGORYY_NAME);
			
			int subCatId=getIntent().getIntExtra(Constants.INTENT_SUB_CATEGORYY_ID, -1);
			setHeaderTitle(catName);
			int localityId=SharedPreferenceManager.getPreferenceValue(BusinessListActivity.this, SharedPreferenceManager.Keys.PREFERRED_LOCATION_ID);
			getBusinessList(catId,subCatId,localityId);

		}
	}

	

	private void initListFragment(){
		FragmentManager fm=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction=fm.beginTransaction();
		fragmentTransaction.add(R.id.storeListFragLay, businessListFragment);
		fragmentTransaction.commit();
}
	private void initMapFragment(){
		FragmentManager fm=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction=fm.beginTransaction();
		fragmentTransaction.add(R.id.storeMapFragLay, businessMapFragment);
		fragmentTransaction.commit();
	}

   private void initElements(){
	   storeListLayout=(RelativeLayout)findViewById(R.id.storeListFragLay);
	   storeMapLayout=(RelativeLayout)findViewById(R.id.storeMapFragLay);
	   storeViewButton=(Button)findViewById(R.id.storeViewButton);
	   storeViewButton.setOnClickListener(this);
	   if(!isMapView){
		storeListLayout.setVisibility(View.VISIBLE);
		storeMapLayout.setVisibility(View.GONE);
		}
	   
   }
   
   private void getBusinessList(int catId,int subCatId,int localityId){
	   if(!isService){
			isService=true;
			mainCategoryHandler.sendEmptyMessage(Constants.SHOW_PROGRESS_DIALOG);
			BusinessListActivityHelper.storeListServiceCall(getApplicationContext(),String.valueOf(catId),String.valueOf(subCatId),String.valueOf(localityId), storeListListener);
			ServiceManager.getStoreList(this,String.valueOf(catId),String.valueOf(subCatId),String.valueOf(localityId), new ServiceResponseListener<StoreResponse>() {
				
				@Override
				public void onSuccess(StoreResponse response) {
							
					
				}
				
				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
				   
				}
			});
		}
   }
   
   public DialogInterface.OnClickListener positiveListener=new DialogInterface.OnClickListener() {
	
	@Override
	public void onClick(DialogInterface dialog, int which) {
		finish();
	}
};
   
@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.storeViewButton:
		if (! isMapView ) {
			if(!isPlayServiceAvailable){
				int requestCode = 10;
				Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
						playServiceStatus,
						BusinessListActivity.this, requestCode);
				dialog.show();
				return;
			}
			 
		}
		isMapView = isMapView ? false : true;
		
		
		if ( isMapView ) {
			storeViewButton.setText(getString(R.string.list_view));
			storeListLayout.setVisibility(View.INVISIBLE);
			storeMapLayout.setVisibility(View.VISIBLE);

		} else {
			storeViewButton.setText(getString(R.string.map_view));
			storeListLayout.setVisibility(View.VISIBLE);
			storeMapLayout.setVisibility(View.INVISIBLE);
		}
		break;

	default:
		break;
	}
	
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home:
		finish();
		break;

	default:
		break;
	}
	return super.onOptionsItemSelected(item);
}

private final UiUpdateListener storeListListener=new UiUpdateListener() {
	
	@Override
	public void OnUpdateUI(Object... object) {
		StoreResponse storeResponse=(StoreResponse)object[0];
		if (storeResponse != null) {
			mainCategoryHandler
					.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			isService = false;
			if (storeResponse.getStatusCode() == Constants.SUCCESS_CODE) {
				List<StoreDetails> businessDetailsList = storeResponse
						.getStoreListResponse();
				businessListFragment
						.setStoreList(businessDetailsList);
				businessListFragment.updateStoreList();
				businessMapFragment.setStoreList(businessDetailsList);
			} else {
				showAlert(BusinessListActivity.this, getString(R.string.alert), getString(R.string.no_details_found), getString(R.string.ok_button), positiveListener, null, null);
			}
		}
		
	}
	
	@Override
	public void OnFailure(String errorMessage) {
		 mainCategoryHandler.sendEmptyMessage(Constants.DISMISS_PROGRESS_DIALOG);
			isService=false;
			showAlert(BusinessListActivity.this, getString(R.string.server_failed), getString(R.string.server_unavailable), getString(R.string.ok_button), positiveListener, null, null);
					
	}
};

}
